<?php
class Controller_Register{
	var $_modelMembers;
	var $_modelAddress;
	
	function Controller_Register(){
		$this->_modelMembers = & FLEA::getSingleton('Model_Members');
		$this->_modelAddress = & FLEA::getSingleton('Model_Address');
	}
	
	function actionIndex(){
		load_language('ui');
		include('App/View/RegisterIndex.php');
	}
	
	function actionSave(){
		$data = array(
			'login_name' => $_POST['login_name'],
			'password' => md5($_POST['password']),
			'email' => $_POST['email'],
			'question' => $_POST['question'],
			'answer' => $_POST['answer'],
			'member_type' => $_POST['type'],
			'regist_time' => date('Y-m-d H:i:s')
		);
		$this->_modelMembers->save($data);
		$id = mysql_insert_id();
		$_SESSION['id'] = $id;
		$this->getUserDir($id);
		$type = $_POST['type'];
		load_language('ui');
		include('App/View/RegisterPart.php');
	}
	
	function actionSavePart(){
		$id = $_SESSION['id'];
		$name = $_POST['name'];
		$phone = $_POST['phone'];
		$mobilephone = $_POST['mobilephone'];
		$str = "UPDATE SHOP_MEMBER SET NAME='".$name."',SEX=".$_POST['sex'].
			",BIRTHDAY='".$_POST['birthday']."',PHONE='".$phone."',MOBILEPHONE='".$mobilephone.
			"', OTHERPHONE='".$_POST['otherphone']."',MSN='".$_POST['msn']."' WHERE ID=".$id;
		$this->_modelMembers->execute($str);
		
		$data = array(
			'member_id' => $id,
			'name' => $name,
			'address' => $_POST['address'],
			'zip' => $_POST['zip'],
			'mobilephone' => $mobilephone,
			'phone' => $phone
		);
		$this->_modelAddress->save($data);
//		load_language('ui');
//		include('App/View/RegisterPart.php');
	}
	
	function getUserDir($userId){
	 	$oneLevel=$userId / 10000;
		$twoLevel=$userId / 100;
	 	$dir="d:/userfiles";
	 	if(!is_dir($dir))
	  		mkdir($dir,0777);
	 	$dir.="/".floor($oneLevel);
	 	if(!is_dir($dir))
	  		mkdir($dir,0777);
	 	$dir.="/".floor($twoLevel);
	 	if(!is_dir($dir))
	  		mkdir($dir,0777);
	 	$dir.="/".$userId."/";
	 	if(!is_dir($dir))
	  		mkdir($dir,0777); 
	 	return $dir;
	}
}
?>